//
//  HMNavigationViewController.swift
//  Weibo
//
//  Created by 黄亦寒 on 16/9/19.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit

class HMNavigationViewController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //添加边缘滑动手势
        self.interactivePopGestureRecognizer?.delegate = self
        // Do any additional setup after loading the view.
    }

    //重写push方法监听导航视图控制器里面的子视图控制器及数量
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if viewControllers.count > 0 {
            
            if viewControllers.count == 1 {
                //获取第0个控制器的title
                let title = viewControllers.first!.title!
                //第一个子控制器的左边返回按钮显示“我的”
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title:title,imageName:"navigationbar_back_withtext",target:self,action:#selector(backAction))
            }else{
                //其他子控制器返回按钮显示“返回”
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title:"返回",imageName:"navigationbar_back_withtext",target:self,action:#selector(backAction))
            }
            //除了第0个控制器所有控制器的标题显示“当前为第x级控制器”
            viewController.title = "当前为第\(viewControllers.count+1)级控制器"
            //隐藏底部tabbar
            viewController.hidesBottomBarWhenPushed = true
        }
        
        //走完这个方法控制器的数量才加1
        super.pushViewController(viewController, animated: animated)
        
    }
    
    //返回按钮的方法
    func backAction() {
        //跳回上一级控制器
        popViewController(animated: true)
    }
    
    //判断是否处理手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        //如果是根控制器不处理手势
        if viewControllers.count == 1 {
            return false
        }
        return true
    }
    

}
